local ngx = ngx
local require = require
local log     = require('modules.commhm.log')
local utils   = require('modules.commhm.utils')

local os      = os
local os_time = os.time
local os_date = os.date

local FOO = {
    _VERSION = '0.0.1',

    -- 运营日志错误日志
    OPLOG_ERRLOG_FILE = 'oplog_error',

    -- 运营日志文件名前缀
    OPLOG_FILE_PREFIX = 'op_record_',
}

FOO.record = function(api, params, type_, ext_)
    if not params or not api then
        return
    end

    local record = {
        uin             = ngx.ctx.m_params.uin,
        platform        = ngx.ctx.m_params.platform,
        channel  		= ngx.ctx.m_params.apiid,
        login_channel   = ngx.ctx.m_params.platid or ngx.ctx.m_params.apiid,
        version  		= ngx.ctx.m_params.ver,
        country  		= ngx.ctx.m_params.country,
        language 		= ngx.ctx.m_params.lang,
        ts  	  		= os_time(),
        dt  	  		= os_date("%Y-%m-%d", os_time()),
        ip				= ngx.ctx.xff or ngx.var.remote_addr or 'nil',
        params          = params,        --json格式
        api             = api,           --埋点接口名称
        type            = type_,         --可变
        ext             = ext_,          --可变
    }

    -- 容错:json解析失败就输出详情到对应的日志中
    local record_str = utils.table2json(record)
    if "" == record_str then
        log.day_list_clean(FOO.OPLOG_ERRLOG_FILE, utils.to_str(record))
    end

    log.day_list_clean(FOO.OPLOG_FILE_PREFIX .. ngx.worker.id(), record_str)
end

return FOO